/**
 * \file  main.cpp
 * \brief Main Program.
 *
 * ASO: Antiviral Sequence Optimizer
 * <http://aso.googlecode.com/>
 * Copyright (C) 2010 Velez Ezequiel, Peralta Andres, FuDePAN
 *               2012 Hugo Arregui, FuDePAN
 *
 * This file is part of the ASO project.
 *
 * Contents:      CPP file for ASO Main Program.
 *
 * System:         ASO
 * Language:       C++
 *
 * Author:         Velez Ezequiel, Peralta Andres
 * E-Mail:
 *
 * ASO is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * ASO is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with ASO.  If not, see <http://www.gnu.org/licenses/>.
 *
 */

#include "inc/gui_params.h"

int main(int argc, char** argv)
{
    int TOTAL_PARAMS = 5;
    AntiviralSet antiviralSet;
    biopp::PseudoNucSequence nucSeq;

    /*! Mostrar la ayuda. */
    if (mili::to_string(argv[1]) == "--help")
    {
        gui_help("aso.help");
    }
    else
    {
        try
        {
            if (argc < TOTAL_PARAMS) throw BadUseOfGUI("Incorrect Parameters. Try aso --help");


            load_sequence(argv[1], nucSeq);
            DataBase DB(argv[2]);
            DB.get_antivirals(antiviralSet);
            AVIterator BDI(antiviralSet.antivirals);
            PluginAdmin* PA = new PluginAdmin;


            print_sequence(nucSeq);
            std::string observations;
            std::string outFileName(argv[5]);
            std::string outFilePath(outFileName + ".trp"); //"../outputs/" +

            std::cout << "-- Observations for this Ranking?: " << std::endl;
            std::getline(std::cin, observations);

            try
            {
                PA->load_plugin(argv[4]);
                AntiviralsNames rejectedAntivirals;

                if (!PA->check_plugin(BDI, rejectedAntivirals))
                {
                    std::cerr << "Error: plugin and Data Base incomplatibles." << std::endl;
                }
                else
                {
                    TherapyRanker rankerObs(atoi(argv[3]));

                    ParameterList params;                     //! Inicialización de parametros
                    PA->get_params(params);
                    gui_plugin(params);
                    mili::delete_container(params);

                    TherapyGenImpl G(rankerObs.get_observer(), antiviralSet, PA);
                    G.generateTherapies(nucSeq);
                    gui_print_ranking(rankerObs, outFilePath, observations);
                    std::cout << "Ranking created at: " << outFilePath << std::endl;
                }
                PA->unload_plugin();
            }
            catch (const PluginError& e)
            {
                std::cerr << "Cannot load plugin" << std::endl;
            }
            delete PA;
        }
        catch (const BadUseOfGUI& exc)
        {
            std::cerr << exc.what() << std::endl;
        }
        catch (const BadValue& exc)
        {
            std::cerr << exc.what() << std::endl;
        }
        catch (const SystemError& exc)
        {
            std::cerr << exc.what() << std::endl;
        }
        catch (const PluginError& exc)
        {
            std::cerr << exc.what() << std::endl;
        }
    }
    return 0;
}
